package com.zle.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class InterceptTest implements HandlerInterceptor{

	/**
	 *执行顺序: preHandle-->control-->postHandle-->afterCompletion
	 * 
	 */
	
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		System.out.println("preHandle...");
		if(request.getAttribute("user") == null){
			//当没有权限时候跳转到登陆界面
			request.getRequestDispatcher("/login_show").forward(request, response);
			return false;
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		//强制修改了返回值
		modelAndView.setViewName("forward:/main");
		System.out.println("postHandle...");
		
	}
	/**
	 * 该方法主要做销毁工作,比如流的关闭,资源释放等操作
	 */
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("afterCompletion...");
		
	}

}
